/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * tameshi
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
 * tameshi is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tameshi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_engine.h"

void TestEngine::
init_test_engine ( Glib::RefPtr<Gtk::Builder> _bldr )
{
	_timer = 0;
	_nrDom = 0;
	_paused = false;

	// toolbar
	_bldr->get_widget ( "test_scmult_domande_toolitem_label2", _domandaNumLabel );
	_bldr->get_widget ( "test_scmult_domande_toolitem_label4", _domandeTotLabel );
	_bldr->get_widget ( "test_scmult_tempo_toolitem_label2", _tempoLabel );
	_bldr->get_widget ( "test_scmult_salta_toolbutton", _skipToolButton );
	_bldr->get_widget ( "test_scmult_pausa_toolbutton", _pauseToolButton );
	_bldr->get_widget ( "test_scmult_edit_toolbutton", _editToolButton );
	// main
	_bldr->get_widget ( "test_scmult_domanda_textview", _domandaTxtView);
	_domandaTxtBfr = _domandaTxtView->get_buffer ();
	Glib::RefPtr<Gtk::TextBuffer::TagTable> table = _domandaTxtBfr->get_tag_table ();
	bool shouldAddTags = ( table->get_size () == 0 );
	if ( shouldAddTags )
	{
		add_bold_tag ( _domandaTxtBfr );
		add_underline_tag ( _domandaTxtBfr );
		add_superscript_tag ( _domandaTxtBfr );
		add_highlight_tag ( _domandaTxtBfr );
	}
}

void TestEngine::
connect_toolbar_signals ( void )
{
	_testWinConnection = _testWin->signal_hide ().connect (sigc::mem_fun (*this, &TestEngine::on_window_closed) );
	_skipToolConnection = _skipToolButton->signal_clicked ().connect (sigc::mem_fun (*this, &TestEngine::on_skip_clicked) );
	_pauseToolConnection = _pauseToolButton->signal_clicked ().connect (sigc::mem_fun (*this, &TestEngine::on_pause_clicked) );
	_editToolConnection = _editToolButton->signal_clicked ().connect (sigc::mem_fun (*this, &TestEngine::on_edit_clicked) );
}

bool TestEngine::
on_timer_fired ( void )
{
	_timer++;
	_tempoLabel->set_text ( Glib::ustring::compose ( "%1 : %2", Glib::ustring::format ( setfill(L'0'), setw(2), _timer / 60 ), Glib::ustring::format ( setfill(L'0'), setw(2), _timer % 60 ) ) );
	return true;
}

void TestEngine::
on_skip_clicked ( void )
{
	_timerConnection.disconnect ();
	next_question ();
}

void TestEngine::
on_pause_clicked ( void )
{
	if ( _paused )
	{
		continue_test ();
		_timerConnection = Glib::signal_timeout ().connect_seconds (sigc::mem_fun (*this, &TestEngine::on_timer_fired), 1 );
	}
	else
		pause_test ();
}

